package com.yiren.dbaa.module.project_edit.view.fragment;

import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.event.FragmentRefreshEvent;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.project_edit.contract.GroupLightContract;
import com.yiren.dbaa.module.project_edit.presenter.GroupLightPresenter;
import com.yiren.dbaa.view.adapter.GroupLightNameAdapter;
import com.yiren.dbaa.view.adapter.SingleLightAdapter;
import com.yiren.dbaa.view.dragsortlistview.DragSortListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/8/19 8:55
 * @description: 灯光组合fragment
 */
public class GroupLight_Fragment extends LazyLoadFragment implements GroupLightContract.View {

    @BindView(R.id.gv_grouplight_name)
    GridView gvGrouplightName;
    @BindView(R.id.tv_add_group)
    TextView tvAddGroup;
    @BindView(R.id.tv_delete_group)
    TextView tvDeleteGroup;
    @BindView(R.id.dragsortlistview_listview)
    DragSortListView dragsortlistviewListview;
    @BindView(R.id.ll_no_groplight)
    LinearLayout llNoGrouplight;
    @BindView(R.id.tv_no_data_tip)
    TextView tvNoDataTip;
    @BindView(R.id.ll_grouplight_name)
    LinearLayout llGrouplightName;
    @BindView(R.id.tv_add_singlelight)
    TextView tvAddSinglelight;


    private GroupLightContract.Presenter groupLightPresenter;
    private GroupLightNameAdapter groupLightNameAdapter;//灯光组合适配器
    private SingleLightAdapter singleLightAdapter;//单项灯光列表适配器
    private List<LightCombinationEntity> groupLightList;//灯光组合list
    private int selectGroupLightIndex = 0;//选择的灯光组合序号
    private SiteEntity site;
    private boolean isAddSingleLightBtn = true;

    @Override
    protected int setContentView() {
        return R.layout.fragment_group_light;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        site = (SiteEntity) getArguments().getSerializable("site");
        dragsortlistviewListview.setDropListener(onDrop);
        groupLightPresenter = new GroupLightPresenter(getActivity());
        groupLightPresenter.loadGroupLightList(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        site = (SiteEntity) UserDbController.getInstance(false).queryById(site.getSiteCode(), SiteEntity.class);
        groupLightPresenter.loadGroupLightList(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(FragmentRefreshEvent event) {
        groupLightPresenter.loadGroupLightList(this);
    }

    // 拖动排序监听
    public DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            groupLightPresenter.moveSingleLight(site, groupLightList.get(selectGroupLightIndex), from, to);
        }
    };

    @Override
    public void loadGropLight(List<LightCombinationEntity> lightCombinationList) {
        if (lightCombinationList.size() <= 0) {
            llGrouplightName.setVisibility(View.GONE);
            tvAddSinglelight.setText("+ 添加组合");
            isAddSingleLightBtn = false;
            dragsortlistviewListview.setVisibility(View.GONE);
            llNoGrouplight.setVisibility(View.VISIBLE);
            tvNoDataTip.setText("当前灯光组合列表为空，请先添加组合灯光！");
            tvDeleteGroup.setBackgroundColor(getResources().getColor(R.color.bg_btn_gray));
            tvDeleteGroup.setClickable(false);
        } else {
            llGrouplightName.setVisibility(View.VISIBLE);
            tvAddSinglelight.setText("+ 添加单项灯光");
            isAddSingleLightBtn = true;
            dragsortlistviewListview.setVisibility(View.VISIBLE);
            llNoGrouplight.setVisibility(View.GONE);
            tvDeleteGroup.setBackground(getResources().getDrawable(R.drawable.btn_red_selector));
            tvDeleteGroup.setClickable(true);
            groupLightList = lightCombinationList;
            groupLightNameAdapter = new GroupLightNameAdapter(getActivity(), groupLightList);
            gvGrouplightName.setAdapter(groupLightNameAdapter);
            singleLightAdapter = new SingleLightAdapter(getActivity(), groupLightList.get(selectGroupLightIndex).getSingleLightEntities(), 2);
            dragsortlistviewListview.setAdapter(singleLightAdapter);

            groupLightNameAdapter.changeSelected(selectGroupLightIndex);
            if (groupLightList.get(selectGroupLightIndex).getSingleLightEntities().size() <= 0) {
                dragsortlistviewListview.setVisibility(View.GONE);
                llNoGrouplight.setVisibility(View.VISIBLE);
                tvNoDataTip.setText("当前灯光组合单项灯光列表为空，请先给该组合添加单项灯光！");
            } else {
                dragsortlistviewListview.setVisibility(View.VISIBLE);
                llNoGrouplight.setVisibility(View.GONE);
                singleLightAdapter.reflesh(groupLightList.get(selectGroupLightIndex).getSingleLightEntities());
            }

            //点击选择某项灯光组合
            groupLightNameAdapter.setOnItemClickListener(new GroupLightNameAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    selectGroupLightIndex = position;
                    groupLightNameAdapter.changeSelected(position);
                    if (groupLightList.get(selectGroupLightIndex).getSingleLightEntities().size() <= 0) {
                        dragsortlistviewListview.setVisibility(View.GONE);
                        llNoGrouplight.setVisibility(View.VISIBLE);
                    } else {
                        dragsortlistviewListview.setVisibility(View.VISIBLE);
                        llNoGrouplight.setVisibility(View.GONE);
                        singleLightAdapter.reflesh(groupLightList.get(selectGroupLightIndex).getSingleLightEntities());
                    }


                }
            });


            //点击替换某单项灯光
            singleLightAdapter.setOnItemModifyClickListener(new SingleLightAdapter.OnItemModifyClickListener() {
                @Override
                public void onItemModifyClick(View view, int position) {
                    groupLightPresenter.replaceSingleLight(site, groupLightList.get(selectGroupLightIndex), position);

                }
            });

            //点击删除某项灯光
            singleLightAdapter.setOnItemDeleteClickListener(new SingleLightAdapter.OnItemDeleteClickListener() {
                @Override
                public void onItemDeleteClick(View view, int position) {
                    groupLightPresenter.deleteSingleLightToGroup(site, groupLightList.get(selectGroupLightIndex), position);

                }
            });

        }
    }

    @Override
    public void refreshAdapter(LightCombinationEntity lightCombination) {
        dragsortlistviewListview.setVisibility(View.VISIBLE);
        llNoGrouplight.setVisibility(View.GONE);
        singleLightAdapter.reflesh(lightCombination.getSingleLightEntities());
    }

    @OnClick({R.id.tv_add_group, R.id.tv_add_singlelight, R.id.tv_delete_group})
    public void onClick(View view) {
        switch (view.getId()) {
            //添加组合
            case R.id.tv_add_group:
                groupLightPresenter.addGroupLight(site, new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        selectGroupLightIndex = (int) result;
                        groupLightPresenter.loadGroupLightList(GroupLight_Fragment.this);
                    }

                    @Override
                    public void fail(String error) {
                        selectGroupLightIndex = Integer.parseInt(error);
                        groupLightPresenter.loadGroupLightList(GroupLight_Fragment.this);
                    }
                });
                break;
            //组合添加单项灯光
            case R.id.tv_add_singlelight:
                if (isAddSingleLightBtn) {
                    groupLightPresenter.addSingleLightToGroup(site, groupLightList, selectGroupLightIndex);
                } else {
                    groupLightPresenter.addGroupLight(site, new CommonCallBack() {
                        @Override
                        public void success(Object result) {
                            selectGroupLightIndex = (int) result;
                            groupLightPresenter.loadGroupLightList(GroupLight_Fragment.this);
                        }
                    });
                }
                break;
            //点击删除某项灯光组合
            case R.id.tv_delete_group:
                groupLightPresenter.deleteGroupLit(site, groupLightList.get(selectGroupLightIndex), new CommonCallBack() {
                    @Override
                    public void complete() {
                        groupLightList.remove(selectGroupLightIndex);
                        selectGroupLightIndex = 0;
                        groupLightPresenter.loadGroupLightList(GroupLight_Fragment.this);
                    }
                });

                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
